<template>
  <div id="grade">
    <div ref="box" class="box"></div>
    <div class="notFound" v-if="isNull">
      <i class="iconfont icon-LC_icon_tips_fill"></i><span>该考生未参加考试</span>
    </div>
    <!-- 添加普通表格 -->
    <table v-if="!isNull" class="score-table">
      <thead>
      <tr>
        <th>次数</th>
        <th>分数</th>
        <th>答题日期</th>
        <th>科目</th>
      </tr>
      </thead>
      <tbody>
      <tr v-for="(data, index) in tableData" :key="index">
        <td>{{ `第${index + 1}次` }}</td>
        <td>{{ data.score }}</td>
        <td>{{ data.answerDate }}</td>
        <td>{{ data.subject }}</td>
      </tr>
      </tbody>
    </table>
  </div>
</template>


<script>
export default {
  name: "grade",
  data() {
    return {
      isNull: false, //原始数据
      tableDataX: [], //x轴数据 保存次数
      tableDataY: [], //y轴数据 保存分数
      tableData: [] //表格数据
    };
  },
  mounted() {
    this.score();
  },
  methods: {
    score() {
      let studentId = this.$route.query.studentId;
      this.$axios(`/api/score/${studentId}`).then((res) => {
        //根据学生Id查询成绩
        console.log(res);
        if (res.data.code == 200) {
          let rootData = res.data.data;
          rootData.forEach((element, index) => {
            this.tableDataX.push(`第${index + 1}次`);
            this.tableDataY.push(element.etScore);
            this.tableData.push({
              score: element.etScore,
              answerDate: element.answerDate,
              subject: element.subject
            });
          });
          let boxDom = this.$refs["box"];
          let scoreCharts = this.$echarts.init(boxDom);
          let option = {
            xAxis: {
              type: "category",
              data: this.tableDataX,
            },
            yAxis: {
              type: "value",
            },
            series: [
              {
                data: this.tableDataY,
                type: "line",
                itemStyle: { normal: { label: { show: true } } },
              },
            ],
          };
          scoreCharts.setOption(option);
          scoreCharts.on("mouseover", (params) => {
            console.log(params.value);
          });
        } else {
          this.isNull = true;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
#grade {
  position: relative;
  .box{
    width: 600px;
    height: 400px;
  }
  .notFound {
    position: absolute;
    top: 0px;
    left: 0px;
  }
}

.score-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 20px;
}
.score-table th, .score-table td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: center;
}
.score-table th {
  background-color: #f4f4f4;
  font-weight: bold;
}
</style>


